<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Product extends MY_Controller
{
    /**
     * Index Page for this controller.
     */
    public function index()
    {
        $this->load->library('c_product_list');

        $sendData = array();
        $sendData['enterpriseId'] = $this->lg_user['enterpriseId'];
        $sendData['channelId'] = $this->lg_user['channelId'];
        $sendData['userType'] = $this->lg_user['type'];
        $sendData['uid'] = $this->lg_user['id'];
        $sendData['password'] = $this->lg_user['password'];

        //制造接口请求
        $url = $this->c_product_list->set_call_items($sendData);
        $param = $this->c_product_list->get_call_items();
        $rs = $this->restcurl->get($url, $param);
        if ($rs && $rs['ErrorCode'] == 0 && $rs['Success'] == 1) {
            $this->c_product_list->set_back_items($rs['CallInfo']);
        }
        $CallInfo = $this->c_product_list->get_back_items();
        $productList = isset($CallInfo['product_list']) ? $CallInfo['product_list'] : array();

        $cateMenu = $productMenu = array();
        foreach ($productList as $key => $val) {
            if (isset($cateMenu[$val['parent_brand_id']])) {
                $cateMenu[$val['parent_brand_id']]['child'][$val['brand_id']] = array('id' => $val['brand_id'], 'name' => $val['brandname']);
            } else {
                $cateMenu[$val['parent_brand_id']] = array('id' => $val['parent_brand_id'], 'name' => $val['parent_brand_name']);
            }
            //$productMenu[$val['brand_id']][] = $val;
            //$productMenu[$val['parent_brand_id']][] = $val;
        }

        $this->load->library('c_product_tags');
        $_url = $this->c_product_tags->set_call_items($sendData);
        $_param = $this->c_product_tags->get_call_items();
        $_rs = $this->restcurl->get($_url, $_param);
        $_rs = json_decode('{"Success":1,"ErrorCode":0,"ErrorMsg":null,"CallInfo":{"tags":[{"id":1,"parent_id":"0","enterprise_id":"79","title":"\u9ed8\u8ba4\u6807\u7b7e\u5206\u7ec4","child_tag":[{"id":255,"parent_id":"1","enterprise_id":"79","title":"\u5f3a\u70c8\u63a8\u8350"},{"id":536,"parent_id":"1","enterprise_id":"79","title":"\u70ed\u9500\u5546\u54c1"},{"id":547,"parent_id":"1","enterprise_id":"164","title":"\u65b0\u54c1\u4e0a\u67b6"},{"id":615,"parent_id":"1","enterprise_id":"177","title":"\u65b0\u54c1\u4e0a\u67b6"},{"id":616,"parent_id":"1","enterprise_id":"177","title":"\u5f3a\u70c8\u63a8\u8350"},{"id":617,"parent_id":"1","enterprise_id":"177","title":"\u70ed\u9500\u5546\u54c1"},{"id":619,"parent_id":"1","enterprise_id":"178","title":"\u65b0\u54c1\u4e0a\u67b6"},{"id":620,"parent_id":"1","enterprise_id":"178","title":"\u5f3a\u70c8\u63a8\u8350"},{"id":621,"parent_id":"1","enterprise_id":"178","title":"\u70ed\u9500\u5546\u54c1"},{"id":623,"parent_id":"1","enterprise_id":"179","title":"\u65b0\u54c1\u4e0a\u67b6"},{"id":624,"parent_id":"1","enterprise_id":"179","title":"\u5f3a\u70c8\u63a8\u8350"},{"id":625,"parent_id":"1","enterprise_id":"179","title":"\u70ed\u9500\u5546\u54c1"},{"id":627,"parent_id":"1","enterprise_id":"180","title":"\u65b0\u54c1\u4e0a\u67b6"},{"id":628,"parent_id":"1","enterprise_id":"180","title":"\u5f3a\u70c8\u63a8\u8350"},{"id":629,"parent_id":"1","enterprise_id":"180","title":"\u70ed\u9500\u5546\u54c1"},{"id":647,"parent_id":"1","enterprise_id":"185","title":"\u65b0\u54c1\u4e0a\u67b6"},{"id":660,"parent_id":"1","enterprise_id":"186","title":"\u65b0\u54c1\u4e0a\u67b6"},{"id":661,"parent_id":"1","enterprise_id":"186","title":"\u5f3a\u70c8\u63a8\u8350"},{"id":662,"parent_id":"1","enterprise_id":"186","title":"\u70ed\u9500\u5546\u54c1"},{"id":664,"parent_id":"1","enterprise_id":"187","title":"\u65b0\u54c1\u4e0a\u67b6"}]},{"id":537,"parent_id":"0","enterprise_id":"79","title":"\u4ea7\u5730","child_tag":[{"id":538,"parent_id":"537","enterprise_id":"79","title":"\u4e0a\u6d77"},{"id":541,"parent_id":"537","enterprise_id":"79","title":"\u676d\u5dde"}]}]}}
', true);
        if ($_rs && $_rs['ErrorCode'] == 0 && $_rs['Success'] == 1) {
            $this->c_product_tags->set_back_items($_rs['CallInfo']);
        }
        $_CallInfo = $this->c_product_tags->get_back_items();

        $this->data['ApiData'] = array('cateMenu' => $cateMenu, 'productMenu' => $productMenu, 'productList' => $productList);
        $this->data['TagsData'] = $_CallInfo['tags'];

        $this->render('product/index', $this->data);
    }

    public function detail()
    {
        if (!isset($this->data['id'])) {
            redirect('/home/index');
        }

        $this->load->library('c_product_detail');

        $sendData = array();
        $sendData['enterpriseId'] = $this->lg_user['enterpriseId'];
        $sendData['channelId'] = $this->lg_user['channelId'];
        $sendData['userType'] = $this->lg_user['type'];
        $sendData['uid'] = $this->lg_user['id'];
        $sendData['password'] = $this->lg_user['password'];

        //制造接口请求
        $this->c_product_detail->set_resource_id($this->data['id']);
        $url = $this->c_product_detail->set_call_items($sendData);

        $param = $this->c_product_detail->get_call_items();
        $rs = $this->restcurl->get($url, $param);
        if ($rs && $rs['ErrorCode'] == 0 && $rs['Success'] == 1) {
            $this->c_product_detail->set_back_items($rs['CallInfo']);
        }

        $this->data['ApiData'] = $this->c_product_detail->get_back_items();
        $this->render('product/detail', $this->data);
    }

    public function newpro()
    {
        $this->load->library('c_new_product');

        $sendData = array();
        $sendData['enterpriseId'] = $this->lg_user['enterpriseId'];
        $sendData['channelId'] = $this->lg_user['channelId'];
        $sendData['userType'] = $this->lg_user['type'];
        $sendData['uid'] = $this->lg_user['id'];
        $sendData['password'] = $this->lg_user['password'];

        //制造接口请求
        $url = $this->c_new_product->set_call_items($sendData);
        $param = $this->c_new_product->get_call_items();
        $rs = $this->restcurl->get($url, $param);
        if ($rs && $rs['ErrorCode'] == 0 && $rs['Success'] == 1) {
            $this->c_new_product->set_back_items($rs['CallInfo']);
        }
        $CallInfo = $this->c_new_product->get_back_items();
        $productList = isset($CallInfo['product_list']) ? $CallInfo['product_list'] : array();
        $this->data['ProductData'] = $productList;
        $this->render('product/newpro', $this->data);
    }

    public function search()
    {
        $result = array('code' => 201, 'message' => '', 'result' => '');

        $this->load->library('c_product_list');

        $tags = $this->input->get_post('tags');
        $max_price = $this->input->get_post('max_price');
        $min_price = $this->input->get_post('min_price');

        $tags_str = '';
        if (count($tags)) {
            foreach ($tags as $key => $val) {
                $tags_str[] = $key . ':' . implode(',', $val);
            }
        }

        $sendData = array();
        $sendData['enterpriseId'] = $this->lg_user['enterpriseId'];
        $sendData['channelId'] = $this->lg_user['channelId'];
        $sendData['userType'] = $this->lg_user['type'];
        $sendData['uid'] = $this->lg_user['id'];
        $sendData['password'] = $this->lg_user['password'];
        $sendData['tags'] = implode(';', $tags_str);
        $sendData['minPrice'] = $min_price;
        $sendData['maxPrice'] = $max_price;

        //制造接口请求
        $url = $this->c_product_list->set_call_items($sendData);
        $param = $this->c_product_list->get_call_items();
        $rs = $this->restcurl->get($url, $param);
        if ($rs && $rs['ErrorCode'] == 0 && $rs['Success'] == 1) {
            $this->c_product_list->set_back_items($rs['CallInfo']);
        }
        $CallInfo = $this->c_product_list->get_back_items();
        $productList = isset($CallInfo['product_list']) ? $CallInfo['product_list'] : array();

        $this->data['ApiData'] = array('productList' => $productList);
        $this->data['header'] = $this->load->view('public/header', $this->header, TRUE);
        $result['result'] = $this->load->view('product/search', $this->data, TRUE);
        //$result['message'] = implode(';', $tags_str);
        echo json_encode($result);
        die();
    }
}
